/**
 * 
 */
package com.oda.download;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.oda.resultats.util.Resultat;

import android.util.Log;

/**
 * @author zou
 *
 */
public class DownloadResult {
	
	

	public InputStream getUrlData(String url) throws URISyntaxException, ClientProtocolException, IOException {

	    DefaultHttpClient client = new DefaultHttpClient();
	    HttpGet method = new HttpGet(new URI(url));
	    HttpResponse res = client.execute(method);
	    return res.getEntity().getContent();
	}
	
	public Resultat retreiveData(InputStream in){
		Log.i("RETREIVE", "innnnnnnnnnnn");
		Resultat res=new Resultat();
		try {
	        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();       
	        factory.setNamespaceAware(true);
	        XmlPullParser xpp = factory.newPullParser();
	        xpp.setInput(new InputStreamReader(in));

	        xpp.nextTag();
	        xpp.require(XmlPullParser.START_TAG, null, "resultat");
	        while (xpp.nextTag() == XmlPullParser.START_TAG) {
	        	
	        	xpp.require(XmlPullParser.START_TAG, null, "biomasse");		            
	            Float biomasse = Float.valueOf(xpp.nextText());res.setBiomasse(biomasse);		           
	            xpp.require(XmlPullParser.END_TAG, null, "biomasse");
	        	
	        	xpp.nextTag();
	        	xpp.require(XmlPullParser.START_TAG, null, "date");		            
	            String date = xpp.nextText();res.setDate(date);		           
	            xpp.require(XmlPullParser.END_TAG, null, "date");
	            
	        	
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "errorRate");
	            Float errorRate = Float.valueOf(xpp.nextText());res.setErrorRate(errorRate);
	            xpp.require(XmlPullParser.END_TAG, null, "errorRate");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "expAzote");
	            Float expAzote = Float.valueOf(xpp.nextText());res.setExpAzote(expAzote);
	            xpp.require(XmlPullParser.END_TAG, null, "expAzote");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "idResultat");
	            Integer idResultat = Integer.valueOf(xpp.nextText());res.setIdResultat(idResultat);
	            xpp.require(XmlPullParser.END_TAG, null, "idResultat");

	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "idUser");
	            Integer idUser = Integer.valueOf(xpp.nextText());res.setIdUser(idUser);
	            xpp.require(XmlPullParser.END_TAG, null, "idUser");
	            
	            xpp.nextTag();
	            xpp.require(XmlPullParser.START_TAG, null, "lai");
	            Float lai = Float.valueOf(xpp.nextText());res.setLai(lai);
	            xpp.require(XmlPullParser.END_TAG, null, "lai");
	            

	            //results.add(new Datum(id, title));
	            Log.d("VALUE", ""+biomasse+" "+errorRate+" "+" "+expAzote+" "+lai+" ");
	        }
	        xpp.require(XmlPullParser.END_TAG, null, "resultat");
	        return res;
	        
	    } catch (Throwable t) {
	       return null;
	    }
		
	}

}
